/*
 * Copyright 2010 Mark Eschbach.
 *
 * $HeadURL$
 * $Id$
 */
package com.meschbach.cise.iterator;

import com.meschbach.cise.Compute;

/**
 *
 * @author "Mark Eschbach" &lt;meschbach@gmail.com&gt;
 */
public class ComputingIterator<I, O, E extends Exception> implements MIterator<O, E> {

    MIterator<I, E> inputs;
    Compute<I, O, E> computer;

    public ComputingIterator(MIterator<I, E> inputs, Compute<I, O, E> computer) {
        this.inputs = inputs;
        this.computer = computer;
    }

    public boolean hasNext() throws E{
        return inputs.hasNext();
    }

    public O next() throws E {
        I input = inputs.next();
        O output = computer.compute(input);
        return output;
    }

    public void close() {
        if (inputs != null) {
            inputs.close();
        }
        inputs = null;
        computer = null;
    }
}
